package level4.grid_case.sort
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	
	import level4.display.GameDispose;
	import level4.grid_case.grid.Grid;
	import level4.grid_case.grid.Node;
	import level4.iso.IsoUtil;

	/**
	 * 可用于排序的网格.
	 * */
	public class SortedGrid extends GameDispose
	{
		protected var grid:Grid;
		protected var gridSize:int;
		
		public function SortedGrid(cols:int,rows:int,gridSize:int)
		{
			this.gridSize = gridSize;
			
			grid = new Grid(cols,rows);
			
			assignDefault();
		}
		
		public function assignDefault():void{
			var arr:Array = [];
			for(var i:int = 0;i<grid.numCols;i++){
				for(var j:int=0;j<grid.numRows;j++){
					var gd:GameDispose = new GameDispose();
					grid.getNode(i,j).tag = gd;
					arr.push(grid.getNode(i,j));
				}
			}
			arr.sort(function(a:Node,b:Node):int{
				var r1:int =(a.x+a.y)-(b.x+b.y); 
				if(r1 == 0){
					return a.y - b.y;
				}
				return r1; 
			});
			for(i=0;i<arr.length;i++){
				addChild(arr[i].tag);
			}
			arr.length = 0;
		}
		override public function toString():String{
			var str:String = "";
			for(var i:int = 0;i<grid.numCols;i++){
				for(var j:int=0;j<grid.numRows;j++){
					str += grid.getNode(i,j).toString();
				}
			}
			return str;
		}
		
	}
}